package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.entity.User;
import com.zhiwen.service.UserFollowService;
import com.zhiwen.domain.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户关注控制器
 */
@Tag(name = "用户关注", description = "用户关注相关接口")
@RestController
@RequestMapping("/follows")
@RequiredArgsConstructor
public class UserFollowController {

    private final UserFollowService userFollowService;

    /**
     * 关注用户
     */
    @Operation(summary = "关注用户", description = "关注指定用户，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "关注成功")
    @PostMapping("/user/{followUserId}")
    @SaCheckLogin
    public Result<Boolean> followUser(
            @Parameter(description = "被关注用户ID") @PathVariable Long followUserId) {
        boolean result = userFollowService.followUser(followUserId);
        return Result.success(result);
    }

    /**
     * 取消关注
     */
    @Operation(summary = "取消关注", description = "取消关注指定用户，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "取消成功")
    @DeleteMapping("/user/{followUserId}")
    @SaCheckLogin
    public Result<Boolean> unfollowUser(
            @Parameter(description = "被取消关注用户ID") @PathVariable Long followUserId) {
        boolean result = userFollowService.unfollowUser(followUserId);
        return Result.success(result);
    }

    /**
     * 检查是否已关注
     */
    @Operation(summary = "检查是否已关注", description = "检查当前登录用户是否已关注指定用户，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "检查成功")
    @GetMapping("/user/{followUserId}/check")
    @SaCheckLogin
    public Result<Boolean> checkFollowed(
            @Parameter(description = "被检查用户ID") @PathVariable Long followUserId) {
        boolean result = userFollowService.isFollowed(followUserId);
        return Result.success(result);
    }

    /**
     * 获取关注列表
     */
    @Operation(summary = "获取关注列表", description = "分页获取指定用户的关注列表")
    @Parameters({
        @Parameter(name = "userId", description = "用户ID"),
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("/user/{userId}/following")
    public Result<Page<UserVO>> getFollowingList(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<User> page = new Page<>(current, size);
        Page<UserVO> userVOPage = userFollowService.getFollowingList(userId, page);
        return Result.success(userVOPage);
    }

    /**
     * 获取粉丝列表
     */
    @Operation(summary = "获取粉丝列表", description = "分页获取指定用户的粉丝列表")
    @Parameters({
        @Parameter(name = "userId", description = "用户ID"),
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("/user/{userId}/followers")
    public Result<Page<UserVO>> getFollowerList(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<User> page = new Page<>(current, size);
        Page<UserVO> userVOPage = userFollowService.getFollowerList(userId, page);
        return Result.success(userVOPage);
    }
}
